package com.wind.cloud.common.util;


import com.wind.cloud.common.constant.ErrorCodes;
import com.wind.cloud.common.exception.IBusinessException;

import java.io.Serializable;

/**
 *
 */
public class ApiResult<T> implements Serializable {

    private Integer code;

    private String msg;

    private T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> ApiResult<T> build(IBusinessException ex) {
        ApiResult<T> result = new ApiResult<>();
        result.setCode(ex.getCode());
        result.setMsg(ex.getMessage());
        return result;
    }

    public static <T> ApiResult<T> build(Integer code, String msg, T data) {
        ApiResult<T> result = new ApiResult<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static <T> ApiResult<T> error(Integer code, T data) {
        return build(code, "error", data);
    }

    public static <T> ApiResult<T> error(Integer code, String msg) {
        return build(code, msg, null);
    }

    public static <T> ApiResult<T> error(Integer code, String msg, T data) {
        return build(code, msg, data);
    }

    public static <T> ApiResult<T> success() {
        return build(ErrorCodes.SUCCESS, "success", null);
    }

    public static <T> ApiResult<T> success(T data) {
        return build(ErrorCodes.SUCCESS, "success", data);
    }

    public static <T> ApiResult<T> success(String msg, T data) {
        return build(ErrorCodes.SUCCESS, msg, data);
    }
}
